<?php
/**
 * Smart Library - Smart Framework
 * http://smart-framework.highet.eu
 *
 * @category   SmartL
 * @package    SmartL_Doctrine
 * @subpackage Template
 * @copyright  Copyright (c) 2005-2008 Smart Framework team - Stepan Sindelar, Vojtech Vit
 * @license    http://www.opensource.org/licenses/bsd-license.php	New BSD License
 */

/**
 * @see Doctrine_Record_Generator
 */
require_once ('Doctrine/Record/Generator.php');

/**
 * Model for translation records of internationalisation template. 
 *
 * @package SmartL_Doctrine
 * @subpackage Template
 */
class SmartL_Doctrine_Template_I18n_Translation extends Doctrine_Record_Generator {
	protected $_options = array ('className' => '%CLASS%Translation', 'fields' => array (), 'generateFiles' => false, 'table' => false, 'pluginTable' => false, 'children' => array () );
	
	/**
	 * __construct
	 *
	 * @param string $options 
	 * @return void
	 */
	public function __construct($options) {
		$this->_options = array_merge ( $this->_options, $options );
	}
	
	public function initialize(Doctrine_Table $table) {
		parent::initialize ( $table );
		// Set translation table name
		$this->setTableName ( $this->_options ['table']->getTableName () . "_translation" );
	}
	
	public function buildRelation() {
		$this->buildForeignRelation ( 'Translation' );
		$this->buildLocalRelation ();
	}
	
	/**
	 * buildDefinition
	 *
	 * @param object $Doctrine_Table
	 * @return void
	 */
	public function setTableDefinition() {
		if (empty ( $this->_options ['fields'] )) {
			throw new Doctrine_I18n_Exception ( 'Fields not set.' );
		}
		
		// Nezjistil jsem žádný užitek tohodle níže
		// $options = array('className' => $this->_options['className']);
		

		$cols = $this->_options ['table']->getColumns ();
		
		// Projdeme všechny lokalizované sloupce, přidáme je do modelu lokalizace
		// a odstraníme z původního, lokalizovaného modelu
		foreach ( $cols as $column => $definition ) {
			if (in_array ( $column, $this->_options ['fields'] )) {
				$columns [$column] = $definition;
				$this->_options ['table']->removeColumn ( $column );
			}
		}
		
		$this->hasColumns ( $columns );
		
		$this->hasColumn ( 'id', 'integer', null, array ('primary' => true ) );
		$this->hasColumn ( 'langId', 'string', 5, array ('primary' => true ) );
		// Nastavíme ke každému query indexBy na "languageId". Díky tomu se výsledky
		// budou vždy ukládat do asociativního pole s indexy ze sloupce "languageId"
		$this->bindQueryParts ( array ('indexBy' => 'langId' ) );
	}
}
